package com.example.teacherservice.controller;

import com.example.teacherservice.dto.ClassAddDAO;
import com.example.teacherservice.dto.ClassDTO;
import com.example.teacherservice.dto.ClassRelationDTO;
import com.example.teacherservice.entity.TeaClass;
import com.example.teacherservice.service.TeaClassService;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.example.teacherservice.entity.Class;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spoc/tea-class")
public class TeaClassController {
    private final TeaClassService teaClassService;

    public TeaClassController(TeaClassService teaClassService) {
        this.teaClassService = teaClassService;
    }

    @PostMapping("/save")
    public ResponseEntity<?> saveTeaClass(@RequestBody TeaClass teaClass) {
        try {
            TeaClass saved = teaClassService.saveTeaClass(teaClass);
            return ResponseEntity.ok(saved);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/teacher/{teaId}")
    public ResponseEntity<List<TeaClass>> getClassesByTeacher(@PathVariable Long teaId) {
        List<TeaClass> classes = teaClassService.findByTeaId(teaId);
        return ResponseEntity.ok(classes);
    }

    @GetMapping("/class/{classId}")
    public ResponseEntity<List<TeaClass>> getTeachersByClass(@PathVariable Long classId) {
        List<TeaClass> teachers = teaClassService.getTeachersByClass(classId);
        return ResponseEntity.ok(teachers);
    }

    // 创建班级
    @PostMapping
    @Transactional
    public ResponseEntity<?> createClass(@RequestBody ClassAddDAO classAddDAO) {
        try {
            // 参数验证
            if (StringUtils.isEmpty(classAddDAO.getClassName())) {
                throw new IllegalArgumentException("班级名称不能为空");
            }
            if (classAddDAO.getTeacherId() == null) {
                throw new IllegalArgumentException("教师ID不能为空");
            }

            // 调用Service层处理
            Class newClass = teaClassService.createClassWithCourses(classAddDAO);

            return ResponseEntity.ok(newClass);
        } catch (Exception e) {
            System.out.printf("创建班级失败", e);
            return ResponseEntity.internalServerError()
                    .body(Map.of(
                            "error", "创建班级失败",
                            "message", e.getMessage(),
                            "detail", e.toString()
                    ));
        }
    }

    // 添加课程到班级
    @PostMapping("/{classId}/courses")
    public ResponseEntity<Void> addCoursesToClass(
            @PathVariable Long classId,
            @RequestBody List<Long> courseIds) {
        teaClassService.addCoursesToClass(classId, courseIds);
        return ResponseEntity.ok().build();
    }

    // 获取教师班级关系（仅classId）
    @GetMapping("/teacher-relations/{teacherId}")
    public ResponseEntity<List<ClassRelationDTO>> getTeacherClassRelations(
            @PathVariable Long teacherId) {
        List<ClassRelationDTO> relations = teaClassService.getEnhancedTeacherClassRelations(teacherId);
        return ResponseEntity.ok(relations);
    }

    @DeleteMapping("/{classId}")
    @Transactional
    public ResponseEntity<?> deleteClass(@PathVariable Long classId) {
        try {
            // 权限检查（可选）
            // 可以添加检查当前用户是否有权限删除这个班级

            teaClassService.deleteClassWithRelations(classId);
            return ResponseEntity.ok().build();
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body(Map.of(
                            "error", "删除班级失败",
                            "message", e.getMessage()
                    ));
        }
    }



}